package timer

import (
	"jtdevice/api/device/model"
	"jtdevice/pkg/orm"
	"time"

	"github.com/go-co-op/gocron"
)

type dbpart struct {
	scheduler *gocron.Scheduler
}

var DbPart = dbpart{
	scheduler: gocron.NewScheduler(time.UTC),
}

func (o *dbpart) Run() {
	o.scheduler.Every("1h").Do(func() {
		orm.AlterPart(model.Status{}.TableName(), "DTU").WithRange(15, 1)
	})
	o.scheduler.Every("1h").Do(func() {
		orm.AlterPart(model.AlarmDet{}.TableName(), "DTU").WithRange(15, 1)
	})
	o.scheduler.Every("1h").Do(func() {
		orm.AlterPart(model.Alarm{}.TableName(), "DTU").WithRange(15, 1)
	})
	o.scheduler.StartAsync()
}

func (o *dbpart) Release() {
	o.scheduler.Stop()
}
